package guoanmaker.personal.business.service;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.personal.business.model.OperatorCode;
import guoanmaker.personal.business.model.PersonalCode;
import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.repository.OperatorCodeRepository;
import guoanmaker.personal.business.repository.PersonalCodeRepository;
import guoanmaker.personal.helper.common.abstractobj.ApiController;
import guoanmaker.personal.helper.common.util.SMSHelper;
import guoanmaker.personal.helper.common.util.SystemHelper;
/**
 * 
* @ClassName: PersonalCodeService 
* @Description: 验证码service
* @author Yvonne_MGL
* @date 2017年7月6日 下午3:53:22 
*  
 */
@Service
public class PersonalCodeService {
	
	@Resource
	private PersonalCodeRepository personalCodeRepository;
	
	@Autowired
	private  OperatorCodeRepository  operatorCodeRepository;
	
	/**
	 * 
	* @Description: 发送验证码并且将验证码及用户手机号入库 使用@Transactional处理事务
	* @param telephone 用户手机号
	* @param @throws Exception
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:53:37     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
    public ReturnResult sendCode(String telephone) throws Exception{
    	ReturnResult returnResult = new ReturnResult();
    	String url = ApiController.SMS_URL;
		String username = ApiController.SMS_USERNAME;
		String password = ApiController.SMS_PASSWORD;
		String epid = ApiController.SMS_EPID;
		String param = "";
		String code="";
		code=SystemHelper.getSixCode();
		param = "username=" + username + "&password=" + password + "&phone=" + telephone + "&epid=" + epid
				+ "&linkid=&subcode=" + "&message=" + URLEncoder.encode("验证码为:", "gb2312") + code;
		String result = SMSHelper.SendGET(url, param);
		System.err.println("验证码：：：："+result);
		if(result.equals("00")){
			
    		PersonalCode personalCode=new PersonalCode();
    		List<PersonalCode> list=personalCodeRepository.findCodeByTelephone(telephone);
    		if(list!=null&&list.size()>0){
    			personalCode=list.get(0);
    		}
    		personalCode.setCode(code);
    		personalCode.setTelephone(telephone);
    		personalCode.setCreateTimeMoreFiveMin(SystemHelper.getMoreFiveMinTime());
    		personalCodeRepository.save(personalCode);
    		returnResult.setKey("success");
    		returnResult.setValue("验证码发送成功");
    		
		}else if(result.equals("2")){
				returnResult.setKey("error");
	    		returnResult.setValue("鉴权失败");
		}else{
				returnResult.setKey("error");
	    		returnResult.setValue("发送失败");
		}
		return returnResult;
    }
	
	/**
	 * 
	* @Description: 对比验证码是否与数据库存储的一直
	* @param username 用户名,即手机号
	* @param code 验证码
	* @author Yvonne_MGL 
	* @date 2017年7月6日 下午3:55:17     
	* @return ReturnResult    
	* @throws 
	 */
	public ReturnResult verifyCode(String username,String code){
		ReturnResult returnResult=new ReturnResult();
		List<PersonalCode> list=personalCodeRepository.findCodeByTelephone(username);
		if(list==null||list.size()==0){
			returnResult.setKey("error");
    		returnResult.setValue("请先获取验证码");
		}else{
			PersonalCode personalCode=list.get(0);
			if(personalCode.getCode()!=null&&code!=null&&personalCode.getCode().equals(code)){
				returnResult.setKey("success");
        		returnResult.setValue("验证成功");
			}else{
				returnResult.setKey("error");
	    		returnResult.setValue("验证失败");
			}
		}
		
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 发送短信 
	* @param telephone 手机号
	* @param message 信息内容
	* @param @throws Exception
	* @author Yvonne_MGL 
	* @date 2017年8月28日 上午9:39:30     
	* @return void    
	* @throws 
	 */
	public void sendMessage(String telephone,String message) throws Exception{
		String url = ApiController.SMS_URL;
		String username = ApiController.SMS_USERNAME;
		String password = ApiController.SMS_PASSWORD;
		String epid = ApiController.SMS_EPID;
		String param = "";
		param = "username=" + username + "&password=" + password + "&phone=" + telephone + "&epid=" + epid
				+ "&linkid=&subcode=" + "&message=" + URLEncoder.encode(message, "gb2312");
		SMSHelper.SendGET(url, param);
	}
	
	@Transactional
    public ReturnResult sendAPPVeryMessage(String phone) throws Exception{
    	ReturnResult returnResult = new ReturnResult();
    	String url = ApiController.SMS_URL;
		String username = ApiController.SMS_USERNAME;
		String password = ApiController.SMS_PASSWORD;
		String epid = ApiController.SMS_EPID;
		String param = "";
		String code="";
	    String sendingcode = "";
	    code=SystemHelper.getSixCode();
		OperatorCode  operatorCode=new OperatorCode();
		if(phone!=null){
			//保存5分钟后的时间，验证时用now即可
	        long curren = System.currentTimeMillis();
	        curren += 5 * 60 * 1000;
	        Date da = new Date(curren);
	        operatorCode =operatorCodeRepository.selectByObject(code,phone,new Date());
	      
			if(null!=operatorCode&&null!=operatorCode.getCodetext()){
				sendingcode = operatorCode.getCodetext();
				operatorCode.setDate(da);
				operatorCodeRepository.save(operatorCode);
			} else {
				 operatorCode=new OperatorCode();
			}
		if("".equals(sendingcode)){
			        operatorCode.setDate(da);
			        operatorCode.setPhone(phone);
			        operatorCode.setCodetext(code);
			        operatorCodeRepository.save(operatorCode);
			        sendingcode=code;
			}
		}
		param = "username=" + username + "&password=" + password + "&phone=" + phone + "&epid=" + epid
				+ "&linkid=&subcode=" + "&message=" + URLEncoder.encode("国安创客"
						+ "验证码："+sendingcode+"", "gb2312");
		String result = SMSHelper.SendGET(url, param);
		if(result.equals("00")){
    		returnResult.setKey("success");
    		returnResult.setValue(sendingcode);
		}else if(result.equals("2")){
				returnResult.setKey("error");
	    		returnResult.setValue("鉴权失败");
		}else{
				returnResult.setKey("error");
	    		returnResult.setValue("发送失败");
		}
		return returnResult;
    }


}
